﻿using LoadClient.YUAN.C;
using LoadClient.YUAN.E;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace LoadClient.YUAN.F
{
    public partial class FrmMain : Form
    {


        int define_stid = Convert.ToInt32(COP.GetAppSettingsValue("LoadNumber"));

        public FrmMain()
        {
            InitializeComponent();

            //myClientHelperAPI = new UAClientHelperAPI();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //连接PLC
            //PLConnectInitialaziton();
            //UI
            LoadDataBase();

            LoadLanguage();

            StationID.Text = define_stid.ToString("00");
        }


        string language = "CH";

        private void LoadLanguage()
        {
            language = LanguageHelper.GetLanguageStr();
            LanguageHelper.SetControlLanguageText(this);
            this.Text = LanguageHelper.SetFormlLanguageText(this.Text);

        }

        List<LoadInfo> listload = new List<LoadInfo>();

        List<Product> productlist = new List<Product>();

        private void LoadDataBase()
        {
            try
            {
                //加载产品资料库
                DGV_ProductList.Rows.Clear();
                productlist = ApiManager.GetProductList();

                List<Product> productlistvs = ApiManager.GetProductListVisable();
                productlistvs.ForEach(p =>
                DGV_ProductList.Rows.Add(new object[] { p.Pid.ToString("000"), p.Name }));
                //加载工位
                DGV_Load.Rows.Clear();
                listload = ApiManager.GetLoadInfoList(define_stid);
                listload.ForEach(a =>
                DGV_Load.Rows.Add(new object[]
                {
                a.LoadName,
                a.ExeCarID,
                a.ProductID,a.ProductName,a.Quantity,a.ReleaseString
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                MessageBox.Show("服务器连接失败!Server No available connect!", "Operation tips", MessageBoxButtons.OK);
                Application.Exit();
            }
        }


        //int[] globalresult = new int[10000];

        bool faults = false;

        //private void EnqueueString(string v)
        //{
        //    int[] result = String2IntArray(v); globalresult = result;

        //    if (listload.Count == 5)
        //    {
        //        if (define_stid == 1)
        //        {

        //            //1
        //            listload[0].ProductID = result[7920];
        //            listload[0].SerialNumber = result[7921];
        //            listload[0].PainterID = result[7922];
        //            listload[0].Quantity = result[7923];
        //            listload[0].AutoProgram = result[7924];
        //            listload[0].ExeCarID = result[7925];

        //            listload[1].ProductID = result[7926];
        //            listload[1].SerialNumber = result[7927];
        //            listload[1].PainterID = result[7928];
        //            listload[1].Quantity = result[7929];
        //            listload[1].AutoProgram = result[7930];
        //            listload[1].ExeCarID = result[7931];

        //            listload[2].ProductID = result[7932];
        //            listload[2].SerialNumber = result[7933];
        //            listload[2].PainterID = result[7934];
        //            listload[2].Quantity = result[7935];
        //            listload[2].AutoProgram = result[7936];
        //            listload[2].ExeCarID = result[7937];

        //            listload[3].ProductID = result[7938];
        //            listload[3].SerialNumber = result[7939];
        //            listload[3].PainterID = result[7940];
        //            listload[3].Quantity = result[7941];
        //            listload[3].AutoProgram = result[7942];
        //            listload[3].ExeCarID = result[7943];

        //            listload[4].ProductID = result[7944];
        //            listload[4].SerialNumber = result[7945];
        //            listload[4].PainterID = result[7946];
        //            listload[4].Quantity = result[7947];
        //            listload[4].AutoProgram = result[7948];
        //            listload[4].ExeCarID = result[7949];

        //            faults = result[7998] == -1 ? true : false;

        //        }


        //        if (define_stid == 2)
        //        {
        //            //2
        //            listload[0].ProductID = result[7950];
        //            listload[0].SerialNumber = result[7951];
        //            listload[0].PainterID = result[7952];
        //            listload[0].Quantity = result[7953];
        //            listload[0].AutoProgram = result[7954];
        //            listload[0].ExeCarID = result[7955];

        //            listload[1].ProductID = result[7956];
        //            listload[1].SerialNumber = result[7957];
        //            listload[1].PainterID = result[7958];
        //            listload[1].Quantity = result[7959];
        //            listload[1].AutoProgram = result[7960];
        //            listload[1].ExeCarID = result[7961];

        //            listload[2].ProductID = result[7962];
        //            listload[2].SerialNumber = result[7963];
        //            listload[2].PainterID = result[7964];
        //            listload[2].Quantity = result[7965];
        //            listload[2].AutoProgram = result[7966];
        //            listload[2].ExeCarID = result[7967];

        //            listload[3].ProductID = result[7968];
        //            listload[3].SerialNumber = result[7969];
        //            listload[3].PainterID = result[7970];
        //            listload[3].Quantity = result[7971];
        //            listload[3].AutoProgram = result[7972];
        //            listload[3].ExeCarID = result[7973];

        //            listload[4].ProductID = result[7974];
        //            listload[4].SerialNumber = result[7975];
        //            listload[4].PainterID = result[7976];
        //            listload[4].Quantity = result[7977];
        //            listload[4].AutoProgram = result[7978];
        //            listload[4].ExeCarID = result[7979];

        //            faults = result[7998] == -1 ? true : false;
        //        }

        //    }
        //}


        #region 连接PLC

        //private Session mySession;
        //private Subscription mySubscription;
        //private UAClientHelperAPI myClientHelperAPI;
        //private MonitoredItem myMonitoredItem;
        //private int itemCount;

        //private void PLConnectInitialaziton()
        //{
        //    try
        //    {

        //        Console.WriteLine("Ready to start server!");

        //        //找到可用的连接
        //        List<EndpointDescription> connstrlist = new List<EndpointDescription>();
        //        ApplicationDescriptionCollection servers = myClientHelperAPI.FindServers(@"opc.tcp://192.168.10.11");

        //        if (servers.Count == 0) { MessageBox.Show("Couldn't find available server!", "Operation tips", MessageBoxButtons.OK); return; }
        //        foreach (ApplicationDescription ad in servers)
        //        {
        //            foreach (string url in ad.DiscoveryUrls)
        //            {
        //                EndpointDescriptionCollection endpoints = myClientHelperAPI.GetEndpoints(url);
        //                foreach (EndpointDescription ep in endpoints)
        //                {
        //                    string securityPolicy = ep.SecurityPolicyUri.Remove(0, 42); Console.WriteLine(ep.EndpointUrl);
        //                    connstrlist.Add(ep);
        //                }
        //            }
        //        }

        //        //初始化,建立新连接
        //        if (mySession != null && !mySession.Disposed)
        //        {
        //            mySubscription.Delete(true);
        //            myClientHelperAPI.Disconnect();
        //            mySession = myClientHelperAPI.Session;
        //        }
        //        else
        //        {
        //            myClientHelperAPI.KeepAliveNotification += new KeepAliveEventHandler(Notification_KeepAlive);
        //            myClientHelperAPI.Connect((EndpointDescription)connstrlist[0], true, "mm", "123456");
        //            //Extract the session object for further direct session interactions
        //            mySession = myClientHelperAPI.Session;
        //            Console.WriteLine("Connected ok");
        //        }

        //        //订阅数据
        //        if (myMonitoredItem != null) myMonitoredItem = myClientHelperAPI.RemoveMonitoredItem(mySubscription, myMonitoredItem);
        //        itemCount++;
        //        string monitoredItemName = "myItem" + itemCount.ToString();
        //        if (mySubscription == null)
        //        {
        //            mySubscription = myClientHelperAPI.Subscribe(1000);
        //        }

        //        myMonitoredItem = myClientHelperAPI.AddMonitoredItem(mySubscription, BTN_Help.Tag.ToString(), monitoredItemName, 1000);
        //        myClientHelperAPI.ItemChangedNotification += new MonitoredItemNotificationEventHandler(Notification_MonitoredItem);

        //    }
        //    catch (Exception)
        //    {
        //        MessageBox.Show("PLC连接失败!PLC No available connect!", "Operation tips", MessageBoxButtons.OK);
        //        Application.Exit();
        //    }
        //}


        //private void Notification_MonitoredItem(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        //{
        //    //return;
        //    if (this.InvokeRequired)
        //    {
        //        this.BeginInvoke(new MonitoredItemNotificationEventHandler(Notification_MonitoredItem), monitoredItem, e);
        //        return;
        //    }
        //    MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
        //    if (notification == null)
        //    {
        //        return;
        //    }
        //    //finally string
        //    EnqueueString(notification.Value.WrappedValue.ToString().Trim());
        //}


        //private int[] String2IntArray(string datav)
        //{
        //    string[] received_arr = datav.Split('|');
        //    received_arr[0] = "0";
        //    received_arr[received_arr.Length - 1] = "0";
        //    int[] reint = Array.ConvertAll(received_arr, int.Parse);
        //    return reint;
        //}


        //private void Notification_KeepAlive(Session sender, KeepAliveEventArgs e)
        //{
        //    if (this.InvokeRequired)
        //    {
        //        this.BeginInvoke(new KeepAliveEventHandler(Notification_KeepAlive), sender, e);
        //        return;
        //    }
        //    try
        //    {
        //        // check for events from discarded sessions.
        //        if (!Object.ReferenceEquals(sender, mySession))
        //        {
        //            return;
        //        }
        //        // check for disconnected session.
        //        if (!ServiceResult.IsGood(e.Status))
        //        {
        //            //try reconnecting using the existing session state
        //            mySession.Reconnect();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        DialogResult result = MessageBox.Show(@"连接丢失，需要重启！Connect Losed", "Operation tips", MessageBoxButtons.OKCancel);
        //        if (result == DialogResult.OK)
        //        {
        //            Application.Exit();
        //        }
        //        else
        //        {

        //        }
        //    }
        //}

        #endregion

        private void FreshRequest_Tick(object sender, EventArgs e)
        {
            try
            {
                var rlist = ApiManager.GetRealTime(define_stid);
                if (rlist != null)
                {
                    for (int i = 0; i < rlist.Count; i++)
                    {
                        listload[i].ProductID = rlist[i].ProductID;
                        listload[i].SerialNumber = rlist[i].SerialNumber;
                        listload[i].PainterID = rlist[i].PainterID;
                        listload[i].Quantity = rlist[i].Quantity;
                        listload[i].AutoProgram = rlist[i].AutoProgram;
                        listload[i].ExeCarID = rlist[i].ExeCarID;
                    }
                }
                for (int i = 0; i < DGV_Load.Rows.Count; i++)
                {
                    List<Product> list = productlist.Where(a => a.Pid == listload[i].ProductID).ToList();
                    listload[i].ProductName = list.Count == 0 ? " " : list[0].Name;

                    DGV_Load.Rows[i].Cells[1].Value = listload[i].ExeCarID;
                    if (listload[i].ProductID > 0)
                    {
                        DGV_Load.Rows[i].Cells[2].Value = listload[i].ProductID;
                        DGV_Load.Rows[i].Cells[3].Value = listload[i].ProductName;
                        DGV_Load.Rows[i].Cells[4].Value = listload[i].Quantity;
                        DGV_Load.Rows[i].Cells[6].Value = listload[i].SerialNumber;
                    }
                    else
                    {
                        DGV_Load.Rows[i].Cells[6].Value = 0;
                    }
                    //Color
                    bool loadpermit = listload[i].ProductID == 0;
                    DGV_Load.Rows[i].Cells[1].Style.BackColor = loadpermit ? Color.Green : Color.Red;

                    SickStatus.ForeColor = faults ? Color.Red : Color.Green;

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("000" + ex.ToString());
            }
        }



        private void FreshUI(object sender, FormClosedEventArgs e)
        {
            LoadDataBase();
        }

        private void BTN_LoadRecord_Click(object sender, EventArgs e)
        {
            FrmLoadRecord frmload = new FrmLoadRecord();
            frmload.ShowDialog();
        }

        private void DGV_ProductList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowsindex = e.RowIndex; int colindex = e.ColumnIndex; if (rowsindex < 0 || loadselectedrowsindex < 0) return;
            Product pp = ApiManager.GetProductByPID(Convert.ToInt32(DGV_ProductList.Rows[rowsindex].Cells[0].Value));
            if (pp == null) return;
            DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value = pp.Id;
            DGV_Load.Rows[loadselectedrowsindex].Cells[3].Value = pp.Name;
        }

        private void BTN_LoadProduct_Click(object sender, EventArgs e)
        {


        }

        private void BTN_LoadNullH_Click(object sender, EventArgs e)
        {
            //DialogResult result0 = MessageBox.Show("Release?", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            //if (result0 == DialogResult.Cancel) return;
            //if (LoadStation.Text.Equals("01"))
            //{
            //    WriteToPLCLoadstation1(1, 0, 0);
            //    MessageBox.Show("Load completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //}
            //if (LoadStation.Text.Equals("02"))
            //{
            //    WriteToPLCLoadstation1(1, 0, 0);
            //    MessageBox.Show("Load completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //}
        }

        private void BTN_LoadExeCar_Click(object sender, EventArgs e)
        {
            //DialogResult result0 = MessageBox.Show("Release?", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            //if (result0 == DialogResult.Cancel) return;
            //if (LoadStation.Text.Equals("01"))
            //{
            //    WriteToPLCLoadstation2(2, 0, 0);
            //    MessageBox.Show("Load completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //}
            //if (LoadStation.Text.Equals("02"))
            //{
            //    WriteToPLCLoadstation2(2, 0, 0);
            //    MessageBox.Show("Load completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
            //}
        }


        /// <summary>
        /// 单值addresskey+index写入
        /// </summary>
        /// <param name="objv"></param>
        /// <param name="address_key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        //public bool Command2PLC_Object_Return(object objv, string address_key, int index)
        //{
        //    string guid = Guid.NewGuid().ToString();

        //    //地址获取
        //    Command cmd = CommnadManager.GetCommandByKeys(address_key);
        //    string address = string.Format(cmd.NodeString, index);
        //    Console.WriteLine(address);

        //    //记录
        //    CommnadManager.InsertCommandRecord(new CommandRecord("Write Action", "Value:" + objv.ToString(), address, DateTime.Now, guid, "False", cmd.GUIDResult));

        //    //写入PLC
        //    List<string> wvalues = new List<string>(); wvalues.Add(objv.ToString().Trim());
        //    List<string> wnodeIdStrings = new List<string>(); wnodeIdStrings.Add(address);
        //    myClientHelperAPI.WriteValues(wvalues, wnodeIdStrings);//Execute commands
        //    //校验值
        //    string result = myClientHelperAPI.ReadValues(wnodeIdStrings).ElementAt<string>(0);

        //    bool xxx = result.Equals(objv.ToString());
        //    CommnadManager.UpdateCommandRecordGuidResult(xxx.ToString(), guid);
        //    return xxx;

        //}

        /// <summary>
        /// 单值address写入 write
        /// </summary>        
        /// <param name="objv"></param>
        /// <param name="address_key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        //public bool Command2PLC_Object_Return(object objv, string address, int index)
        //{
        //    Console.WriteLine(address);
        //    //记录
        //    ApiManager.InsertCommandRecord(new CommandRecord("Write Action", "Value:" + objv.ToString(), address, DateTime.Now, "11", "False", "True"));
        //    //写入PLC
        //    List<string> wvalues = new List<string>(); wvalues.Add(objv.ToString().Trim());
        //    List<string> wnodeIdStrings = new List<string>(); wnodeIdStrings.Add(address);
        //    myClientHelperAPI.WriteValues(wvalues, wnodeIdStrings);//Execute commands
        //    //校验值
        //    string result = myClientHelperAPI.ReadValues(wnodeIdStrings).ElementAt<string>(0);
        //    bool xxx = result.Equals(objv.ToString());
        //    return xxx;
        //}




        private void ProductQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是数字键，也不是回车键、Backspace键，则取消该输入
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }


        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {


        }


        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            FreshRequest.Stop();
            FreshRequest.Dispose();
        }


        int loadselectedrowsindex = -1;
        private void DGV_Load_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            loadselectedrowsindex = e.RowIndex; int colindex = e.RowIndex; if (loadselectedrowsindex < 0) return;

        }


        private void DGV_Load_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            loadselectedrowsindex = e.RowIndex; int colindex = e.ColumnIndex; if (loadselectedrowsindex < 0) return;
            if (colindex == 3)
            {
                DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value = string.Empty;
                DGV_Load.Rows[loadselectedrowsindex].Cells[3].Value = string.Empty;
            }
        }


        private void BTN_CarIDUsed_Click(object sender, EventArgs e)
        {
            DialogResult result0 = MessageBox.Show("Set ExeCarID?", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result0 == DialogResult.Cancel) return;
            bool x5 = ApiManager.DoCommand((int)CarID.Value, listload[loadselectedrowsindex].EXECarIDNodeString, 0);
        }

        private void BackSpace_Click(object sender, EventArgs e)
        {

            try
            {
                DialogResult result0 = MessageBox.Show("BackSpace?", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result0 == DialogResult.Cancel) return;

                //删除产品号
                bool x1 = ApiManager.DoCommand(0, listload[loadselectedrowsindex].PIDNodeString, 0);

                if (!x1) return;

                int re = ApiManager.InsertLoadReport(new LoadReport
                {
                    StationNumber = loadselectedrowsindex + 1 + (define_stid * 1000),
                    ProductSerial = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[6].Value),
                    FbarId = 0,
                    UID = "0",
                    ExeCarID = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[1].Value),
                    ProductId = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value),
                    ProductName = DGV_Load.Rows[loadselectedrowsindex].Cells[3].Value.ToString() + "  Backspace",
                    ProductPPid = 0,
                    Quantity = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[4].Value),
                    Operator = "1",
                    InsertTime = DateTime.Now
                });
                if (re > 0)
                {
                    DGV_Load.Rows[loadselectedrowsindex].Cells[6].Value = "0";
                    MessageBox.Show("BackSpace completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据不正确！Format Error!", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            }
        }

        private void DGV_Load_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int colindex = e.ColumnIndex;
            #region Release
            if (colindex == 5)
            {
                try
                {
                    DialogResult result0 = MessageBox.Show("Up load?", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (result0 == DialogResult.Cancel
                        || loadselectedrowsindex < 0
                        || Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value) == 0
                        || DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value == null
                        || DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value.ToString() == "")
                        return;

                    if (listload[loadselectedrowsindex].ProductID > 0) { MessageBox.Show("LoadStation has been ready , productid exist!"); return; }
                    ;

                    int serial = ApiManager.GetLoadReportMaxSerial() + 1;
                    int fbarid = 0;
                    string uid = "0";
                    int ccid = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[1].Value);
                    int carid = ccid.Equals(0) ? 1 : ccid;
                    int pid = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[2].Value);
                    string pname = DGV_Load.Rows[loadselectedrowsindex].Cells[3].Value.ToString();
                    Product ppp = ApiManager.GetProductByPID(pid);
                    int ppid = ppp.Ppid;
                    int qty = Convert.ToInt32(DGV_Load.Rows[loadselectedrowsindex].Cells[4].Value);

                    Task.Run(async () =>
                    {

                        bool x1 = ApiManager.DoCommand(pid, listload[loadselectedrowsindex].PIDNodeString, 0);
                        bool x2 = ApiManager.DoCommand(qty, listload[loadselectedrowsindex].QTYNodeString, 0);
                        bool x3 = ApiManager.DoCommand(ppp.PainterProgram, listload[loadselectedrowsindex].PROGRAMNodeString, 0);
                        bool x4 = ApiManager.DoCommand(serial, listload[loadselectedrowsindex].SerialNodeString, 0);
                        bool x5 = ApiManager.DoCommand(carid, listload[loadselectedrowsindex].EXECarIDNodeString, 0);
                        bool x6 = ApiManager.DoCommand(ppp.PainterID, listload[loadselectedrowsindex].PainterIDNodeString, 0);

                        Console.WriteLine(x1);
                        Console.WriteLine(x2);
                        Console.WriteLine(x3);
                        Console.WriteLine(x4);
                        //Console.WriteLine(x5);
                        Console.WriteLine(x6);

                        await Task.Delay(10);

                        //信息写入成功 请求写入RFID
                        if (x1 && x2 && x3 && x5 && x4 && x6)
                        {
                            this.Invoke(new Action(() =>
                            {
                                int re = ApiManager.InsertLoadReport(new LoadReport
                                {
                                    StationNumber = loadselectedrowsindex + 1 + (define_stid * 1000),
                                    ProductSerial = serial,
                                    FbarId = fbarid,
                                    UID = uid,
                                    ExeCarID = carid,
                                    ProductId = pid,
                                    ProductName = pname,
                                    ProductPPid = ppid,
                                    Quantity = qty,
                                    Operator = "1",
                                    InsertTime = DateTime.Now
                                });
                                if (re > 0)
                                {
                                    DGV_Load.Rows[loadselectedrowsindex].Cells[6].Value = serial;
                                    MessageBox.Show("Load completed!", "Operation Tips", MessageBoxButtons.OK, MessageBoxIcon.Question);
                                }
                            }));
                        }

                    });
                }
                catch (Exception)
                {
                    MessageBox.Show("数据不正确！Format Error!", "Operation Tips", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                }
            }
            #endregion


        }

        private void LoadStation_ValueChanged(object sender, EventArgs e)
        {

        }


        private void BTN_Language_Click(object sender, EventArgs e)
        {
            language = COP.GetAppSettingsValue("Language");
            if (language == "EN")
            {
                COP.SetAppSettingsValue("Language", "CH");
            }
            else
            {
                COP.SetAppSettingsValue("Language", "EN");
            }
            LoadLanguage();
        }

        private void BTN_FreshData_Click(object sender, EventArgs e)
        {
            productlist = ApiManager.GetProductList();
            DGV_ProductList.Rows.Clear();
            List<Product> productlistvs = ApiManager.GetProductListVisable();
            productlistvs.ForEach(p =>
            DGV_ProductList.Rows.Add(new object[] { p.Pid.ToString("000"), p.Name }));

        }

    }
}
